package com.cyy;

/**
 * 单项循环链表
 * @author 会玩的洋洋
 * @since 2022-02-22
 * @param <E>
 */
public class SingleCircleLinkedList<E> extends AbstractList<E> {
    /**
     * 头结点
     */
    private Node<E> firstNode;

    @Override
    public void clear() {
        firstNode = null;
        size = 0;
    }

    @Override
    public E get(int index) {
        return node(index).element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = node(index);
        E oldElement = node.element;
        node.element = element;
        return oldElement;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckAdd(index);
        if (index == 0) {
            Node<E> newNode = new Node<>(firstNode, element);
            // 拿到最后一个节点
            Node<E> last = size == 0 ? newNode : node(size - 1);
            last.next = firstNode;
            firstNode = newNode;
        } else {
            Node<E> prev = node(index - 1);
            prev.next = new Node<>(prev.next, element);
        }
        ++size;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        Node<E> prev = null;
        Node<E> node = firstNode;
        if (index == 0) {
            if (size == 1) {
                firstNode = null;
            }else {
                // 删除头结点
                prev = node(size - 1);
                node = firstNode;
                firstNode = node.next;
                prev.next = firstNode;
            }
        } else {
            // 正常情况
            prev = node(index - 1);
            node = prev.next;
            prev.next = node.next;
        }
        --size;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        Node<E> node = firstNode;
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (node.element == null) {
                    return i;
                }
                node = node.next;
            }
        } else {
            for (int i = 0; i < size; i++) {
                if (node.element.equals(element)) {
                    return i;
                }
                node = node.next;
            }
        }
        return FAILED_RETURN;
    }

    private Node<E> node(int index) {
        rangeCheck(index);

        Node<E> node = firstNode;
        for (int i = 0; i < index; i++) {
            node = node.next;
        }
        return node;
    }

    private static class Node<E> {
        Node<E> next;
        E element;

        public Node() {
        }

        public Node(Node<E> next, E element) {
            this.next = next;
            this.element = element;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("SingleCircleLinkedList{")
                .append("firstNode=").append(firstNode);
        Node<E> node = firstNode;
        for (int i = 0; i < size; i++) {
            sb.append(", element=").append(node.element);
            node = node.next;
        }

        sb.append(", size=").append(size).append("}");
        return sb.toString();
    }
}
